package com.student.dao;

import com.student.bean.Student;

import java.sql.Connection;
import java.util.List;

/**
 * @ClassName StudentDAOImpl
 * @Description StudentDAO接口的实现类
 * @Author wk
 * @Date 2022/3/2 22:09
 * @Version 1.0
 */
public class StudentDAOImpl extends BaseDAO<Student> implements StudentDAO {

    /**
     * @author wk
     * @Description 将 student 对象添加到数据库中
     * @Date 10:09 2022/3/3
     * @Param
     * @Return
     */
    @Override
    public int insert(Connection connection, com.student.bean.Student student) {
        String sql = "insert into student(student_id,student_name,student_sex,student_age,student_phone," +
                "student_location,student_card,student_english,student_math,student_java) values(?,?,?,?,?,?,?,?,?,?)";
        int insert = update(connection, sql, student.getStudentId(), student.getName(), student.getSex(),
                student.getAge(), student.getPhone(), student.getLocation(),
                student.getCard(), student.getEnglish(), student.getMath(), student.getJava());
        return insert;
    }

    /**
     * @author wk
     * @Description 根据学生的学号，删除学生信息
     * @Date 10:10 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int delete(Connection connection, String studentId) {
        String sql = "delete from student where student_id = ?";
        int delete = update(connection, sql, studentId);
        return delete;
    }

    /**
     * @author wk
     * @Description 根据学号和字段修改指定学生的指定字段信息
     * @Date 10:22 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int update(Connection connection, String studentId, String key, Object value) {
        String sql = "update student set " + key + "= ? where student_id = ?";
        int update = update(connection, sql, value, studentId);
        return update;
    }

    /**
     * @author wk
     * @Description 针对内存中的 Student 对象，去修改数据库中指定的学生的全部数据
     * @Date 10:10 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int updateAll(Connection connection, String oldStudentId, Student student) {
        String sql = "update student set student_id = ?,student_name = ?,student_sex = ?," +
                "student_age = ?,student_phone = ?,student_location = ?," +
                "student_card = ?,student_english = ?,student_math = ?,student_java = ? where student_id = ?";
        int updateAll = update(connection, sql, student.getStudentId(), student.getName(), student.getSex(), student.getAge(), student.getPhone(), student.getLocation(), student.getCard(),
                student.getEnglish(), student.getMath(), student.getJava(), oldStudentId);
        return updateAll;
    }

    /**
     * 通过账号id，检查学生是否存
     *
     * @param connection 连接
     * @param studentId  学生证
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistById(Connection connection, String studentId) {
        String sql = "select count(*) from student where student_id = ?";
        long count = this.getValue(connection, sql, studentId);
        return count > 0;
    }

    /**
     * 通过身份证号，检查学生是否存
     *
     * @param connection 连接
     * @param card       身份证号
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistByCard(Connection connection, String card) {
        String sql = "select count(*) from student where student_card = ?";
        long count = this.getValue(connection, sql, card);
        return count > 0;
    }

    /**
     * 通过手机号，检查学生是否存在
     *
     * @param connection 连接
     * @param phone      手机号
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistByPhone(Connection connection, String phone) {
        String sql = "select count(*) from student where student_phone = ?";
        long count = this.getValue(connection, sql, phone);
        return count > 0;
    }

    /**
     * @author wk
     * @Description 根据学生学号，查询学生信息
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public com.student.bean.Student getStudentByStudentId(Connection connection, String studentId) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_id = ?";
        Student student = getInstance(connection, sql, studentId);
        return student;
    }

    /**
    * @author wk
    * @Description 通过手机号查询学生信息
    * @Date 16:58 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public Student getStudentByPhone(Connection connection, String phone) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_phone = ?";
        Student student = getInstance(connection, sql, phone);
        return student;
    }

    /**
    * @author wk
    * @Description 通过身份证号查询学生信息
    * @Date 16:59 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public Student getStudentByCard(Connection connection, String card) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_card = ?";
        Student student = getInstance(connection, sql, card);
        return student;
    }

    /**
     * @author wk
     * @Description 查询表中所有记录构成的集合
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public List<com.student.bean.Student> getStudentAll(Connection connection) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student";
        List<Student> forList = getForList(connection, sql);
        return forList;
    }

    /**
     * @author wk
     * @Description 查询数据库中 Student 数据总数目
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public Long getCount(Connection connection) {
        String sql = "select count(*) from student";
        Long value = getValue(connection, sql);
        return value;
    }

    /**
    * @author wk
    * @Description 清空所有学生信息
    * @Date 16:58 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public int clearAll(Connection connection) {
        String sql = "delete from student";
        int clearAll = update(connection, sql);
        return clearAll;
    }
}
